package com.example.transactiondemo.controller;

import com.example.transactiondemo.entity.Userinfo;
import com.example.transactiondemo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.IOException;

@Transactional(rollbackFor = Exception.class,isolation = Isolation.REPEATABLE_READ)
@RestController
@RequestMapping("user")
public class UserController {
    @Autowired
    private UserService userService;


    @PostMapping("/add")
    public String addUser(@RequestBody Userinfo user) throws IOException {

        userService.addUser(user);
        //强制程序抛出异常
        if (true){
            throw new IOException();
        }
        return "User added successfully!";
    }
}
